package com.gec.controller;

import com.gec.dao.BookDao;
import com.gec.dao.BookDao1;
import com.gec.dao.BookDao2;
import com.gec.entity.Book;

import javax.servlet.ServletException;

import javax.servlet.annotation.WebServlet;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**

 - 处理删除客房的请求
 */
@WebServlet("/DeleteBookServlet1")
public class DeleteBookServlet1 extends HttpServlet {
    /**
     - 根据 客房的编号 去数据库中删除对应的图书信息
     */
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //        DeleteBookServlet?id=${book.id}
        //获取用户的id

        String id = req.getParameter("id");
        String name = req.getParameter("name");
        String author = req.getParameter("author");
        String price = req.getParameter("price");

        //调用dao中的删除信息的方法

        //删除成功后 应该去客房列表页面
        String  sql = "insert into custom values (?,?,?,?)";
        //用一个集合来暂存要添加的数据
        ArrayList<Object> objects = new ArrayList<>();
        objects.add(id);
        objects.add(name);
        objects.add(author);
        objects.add(price);

        //创建bookDao来操作
        BookDao2 bookDao = new BookDao2();
        //执行sql语句 传入数据
        bookDao.update(sql,objects);
        BookDao1 bookDao1 = new BookDao1();
        bookDao1.deleteBook(req.getParameter("id"));
        List<Book> books = bookDao1.getBooks();
        req.setAttribute("booklist",books);
        req.getRequestDispatcher("index1.jsp").forward(req,resp);


    }
}

